import wx
from wx.lib.rcsizer import RowColSizer
import co

ADD=wx.NewId()
CANCEL=wx.NewId()


class addprojets(wx.Dialog):
    def __init__(self,parent,id,title,edit,idu,**kwds):
        
        #initialisation
        self.connect = co.co()
        self.title = title
        self.edit = edit
        self.idu = 0
        self.parms = []
        self.params = ['','','','','','','']
        if edit:
            self.parms = self.connect.getprojetparms(idu)
            i = 0
            for elem in self.parms:
                self.params[i] = elem
                i+=1
            self.idu = idu
            
        wx.Dialog.__init__(self,parent,id,title,**kwds)
        
        #create the widgets
        
        #self.nomEmp = wx.RadioBox(self, -1,"Salutation: ", wx.DefaultPosition,wx.DefaultSize,tabPostes,1,wx.RA_SPECIFY_ROWS)
        #self.nomEmp.SetStringSelection(str(self.parms['Nom']))
        print self.params
        self.labNom = wx.StaticText(self, -1, "Nom du Projet: ")
        self.txtNom = wx.TextCtrl(self, -1, str(self.params[1]))
        self.labDuree = wx.StaticText(self, -1, "Duree du projet: ")
        self.txtDuree = wx.TextCtrl(self, -1, str(self.params[2]))
        self.labEff = wx.StaticText(self, -1, "Effectif: ")
        self.txtEff = wx.TextCtrl(self, -1, str(self.params[3]))
        self.labBud = wx.StaticText(self, -1, "Budget: ")
        self.txtBud = wx.TextCtrl(self, -1, str(self.params[4]))
        self.labDeb = wx.StaticText(self, -1, "Date dedut: ")
        self.txtDeb = wx.TextCtrl(self, -1, str(self.params[5]))
        self.labFin = wx.StaticText(self, -1, "Date fin: ")
        self.txtFin = wx.TextCtrl(self, -1, str(self.params[6]))
        self.butsave = wx.Button(self, ADD, "Save")
        self.butcancel = wx.Button(self, CANCEL, "Cancel")
        self.txtNom.SetSize((320, 26))
        self.__do_layout()


    def __do_layout(self):
        
        self.SetPosition([300,250])
        boxl = RowColSizer()
        boxl.Add(self.labNom, row=1, col=1)
        boxl.Add(self.txtNom, row=1, col=2, colspan=2)
        boxl.Add(self.labDuree, row=2, col=1)
        boxl.Add(self.txtDuree, row=2, col=2, colspan=2)
        boxl.Add(self.labEff, row=3, col=1)
        boxl.Add(self.txtEff, row=3, col=2, colspan=2)
        boxl.Add(self.labBud, row=4, col=1)
        boxl.Add(self.txtBud, row=4, col=2, colspan=2)
        boxl.Add(self.labDeb, row=5, col=1)
        boxl.Add(self.txtDeb, row=5, col=2, colspan=2)
        boxl.Add(self.labFin, row=6, col=1)
        boxl.Add(self.txtFin, row=6, col=2, colspan=2)
       
        
        boxb = wx.BoxSizer(wx.HORIZONTAL)
        boxb.Add(self.butsave, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
        boxb.Add(self.butcancel, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
        boxl.Add(boxb, row=8, col=2)
        
        for x in range(1,5):
            boxl.AddSpacer(75,30, pos=(x,1))
            boxl.AddSpacer(380,1, pos=(x,2))
            
        boxl.AddSpacer(75,30,pos=(5,1))
        self.SetAutoLayout(1)
        self.SetSizer(boxl)
        boxl.Fit(self)
        boxl.SetSizeHints(self)
        self.Layout()
        wx.EVT_BUTTON(self, ADD, self.add)
        wx.EVT_BUTTON(self, CANCEL, self.cancel)
 
    def cancel(self,event):
        self.EndModal(0)
        
    def add(self, event):
        msg=''
        ok= True
        self.params[0] = self.txtNom.GetValue()
        self.params[1] = self.txtDuree.GetValue()
        self.params[2] = self.txtEff.GetValue()
        self.params[3] = self.txtBud.GetValue()
        self.params[4] = self.txtDeb.GetValue()
        self.params[5] = self.txtFin.GetValue()
        #if edit mode
        if self.edit:
            #check for duplicates
                #msg,ok = self.connect.duplicedit(self.params[0], self.idu,msg,ok)
                if ok:
                    self.connect.updateprojet(self.params,self.idu) #external program
                    self.EndModal(1)
                else:
                    #display error message
                    error = wx.MessageDialog(self,msg,'Error',wx.OK)
                    error.ShowModal()
                    error.Destroy()    
        #if add mode    
        else:
            #msg,ok = self.connect.duplic(self.params[0],self.params[1],msg,ok)
            if ok:
                self.connect.enterprojet(self.params) #external program
                self.EndModal(1)
            else:
                error = wx.MessageDialog(self,msg,'Error',wx.OK)
                error.ShowModal()
                error.Destroy()
